package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.battle.pve.PVEStage;
import com.xcity.game.battle.pve.template.StageTemplate;
import com.xcity.game.common.Currency;
import com.xcity.game.common.ItemResponse;
import com.xcity.game.item.ItemInfo;
import com.xcity.game.item.ItemTemplate;
import com.xcity.game.item.drop.Drop;
import com.xcity.game.item.drop.impl.ExpDrop;
import com.xcity.game.item.drop.impl.GoldDrop;
import com.xcity.game.shop.ShopItemTemplate;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class PVEStageResponse extends Response {
	
	private int id;
	private int leftTimes; // 剩余可挑战次数
	private int str; // 消耗体力
	private int levelLimit; // 角色等级限制
	private String name; // 关卡名
	private String desc; // 描述(胜利条件)
	private DropInfo rewards; // 可掉落的奖励
	private List<Item> items = Collections.emptyList(); // 可携带的道具
	private int star; // 历史战绩(即星级,0:未通过;1~3:已通过(1~3星))
	private int plotId; // 剧情id
	
	public static class DropInfo {
		private int gold;
		private int exp;
		private List<Drop> items = Collections.emptyList();
		public static final DropInfo EMPTY = new DropInfo();
		public DropInfo() {}
		public DropInfo(List<Drop> drops, float addedRatio) {
			items = new ArrayList<Drop>(drops.size());
			for (int i = 0; i < drops.size(); i++) {
				Drop d = drops.get(i);
				if (d instanceof GoldDrop) {
					gold += ((GoldDrop) d).getGold() * (1 + addedRatio);
				} else if (d instanceof ExpDrop) {
					exp += ((ExpDrop) d).getExp() * (1 + addedRatio);
				} else {
					items.add(d);
				}
			}
		}
		public int getGold() {
			return gold;
		}
		public void setGold(int gold) {
			this.gold = gold;
		}
		public int getExp() {
			return exp;
		}
		public void setExp(int exp) {
			this.exp = exp;
		}
		public List<Drop> getItems() {
			return items;
		}
		public void setItems(List<Drop> items) {
			this.items = items;
		}
	}
	
	public static class Item extends ItemResponse {
		protected int limit; // 携带上限
		protected Currency priceType; // 价格类型(1:现金, 2:钻石)
		protected long price; // 价格
		public Item(int id, int count, String name, String icon, String desc, int limit) {
			super(id, count, name, icon, desc);
			this.limit = limit;
		}
		public int getLimit() {
			return limit;
		}
		public void setLimit(int limit) {
			this.limit = limit;
		}
		public Currency getPriceType() {
			return priceType;
		}
		public void setPriceType(Currency priceType) {
			this.priceType = priceType;
		}
		public long getPrice() {
			return price;
		}
		public void setPrice(long price) {
			this.price = price;
		}
	}

	public PVEStageResponse(int serial, Player player, PVEStage stage) {
		super(serial);
		this.id = stage.getId();
		this.leftTimes = stage.getLeftTimes(player);
		StageTemplate template = stage.getTemplate();
		this.str = template.getCostStr();
		this.levelLimit = template.getLevelLimit();
		this.name = template.getName();
		this.desc = template.getDesc();
		this.rewards = new DropInfo(template.getRewardsDrop()/*stage.getStar() > 0 ? template.getRewardsDrop() : template.getTheBestPassRewardDrop()*/, 0f);
		List<ItemInfo> canCarrys = stage.getTemplate().getCanCarryItemsList();
		if (canCarrys != null && !canCarrys.isEmpty()) {
			this.items = new ArrayList<Item>(canCarrys.size());
			for (int i = 0; i < canCarrys.size(); i++) {
				ItemInfo ref = canCarrys.get(i);
				ItemTemplate itemTemplate = ref.getTemplate();
				int count = player.getBag().countOf(ref.getId()); // 统计背包已有数量
				ShopItemTemplate shopItemTemplate = App.getApp().getServiceManager().get(TemplateService.class).get(ShopItemTemplate.class, ref.getId());
				Item item = new Item(ref.getId(), count < ref.getCount() ? count : ref.getCount(), itemTemplate.getName(), itemTemplate.getIcon(), itemTemplate.getDesc(), ref.getCount());
				if (shopItemTemplate != null) {
					item.setPriceType(shopItemTemplate.getPriceType());
					item.setPrice(shopItemTemplate.getPrice());
				}
				this.items.add(item);
			}
		}
		this.star = stage.getStar();
		this.plotId = stage.getTemplate().getPlotId();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getLeftTimes() {
		return leftTimes;
	}

	public void setLeftTimes(int leftTimes) {
		this.leftTimes = leftTimes;
	}

	public int getStr() {
		return str;
	}

	public void setStr(int str) {
		this.str = str;
	}

	public int getLevelLimit() {
		return levelLimit;
	}

	public void setLevelLimit(int levelLimit) {
		this.levelLimit = levelLimit;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public DropInfo getRewards() {
		return rewards;
	}

	public void setRewards(DropInfo rewards) {
		this.rewards = rewards;
	}

	public List<Item> getItems() {
		return items;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}

	public int getStar() {
		return star;
	}

	public void setStar(int star) {
		this.star = star;
	}

	public int getPlotId() {
		return plotId;
	}

	public void setPlotId(int plotId) {
		this.plotId = plotId;
	}
}
